package cn.lf.android.lawfirm.home.controller;

import android.content.Context;
import android.util.Log;

import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.home.modle.HomeGroups;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.JsonBeans;
import cn.lf.android.lawfirm.utils.SPUtil;


public class HomeController extends BaseController {
    HomeCallback mHomeCallback;
    private Context context;

    public HomeController(HomeCallback callback, Context context) {
        this.mHomeCallback = callback;
        this.context = context;
    }

    public void onHome() {
//        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("deviceType", "1");
        params.put("Authorization", spUtil.getAccessToken());
        new MyOkHttpClient().doGet(AppConfig.HOME, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("首页66===" + response);
                        JSONObject myJsonObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                arraylist = myJsonObject.getJSONArray("responsObject")
                                        .toString();
                                List<HomeGroups> beans = JsonBeans
                                        .getJsonList(
                                                arraylist,
                                                new TypeToken<List<HomeGroups>>() {
                                                });
                                List<HomeGroups> beans2 = new ArrayList<HomeGroups>();
                                for (HomeGroups gropup : beans) {
                                    //暂时没有1,6,8,  1(录入案源) 6(增级申请) 8(工作日志)
                                    if (gropup.getMenuID() == 2 || gropup.getMenuID() == 3 || gropup.getMenuID() == 4 || gropup.getMenuID() == 5 || gropup.getMenuID() == 10 || gropup.getMenuID() == 7 || gropup.getMenuID() == 9) {
                                        beans2.add(gropup);
                                    }
                                }
                                for (int i = beans2.size(); i < 8; i++) {
                                    HomeGroups groups = null;
//                                    beans2.add(groups);//先不补位
                                }
                                mHomeCallback.onSuccessed(beans2);
                            } else {
                                mHomeCallback
                                        .onFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        Log.e("response=首页66==", "response=首页=失败");
                        mHomeCallback.onFailed();
//                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * 首页回调接口
     */
    public interface HomeCallback {
        /**
         * 首页请求成功
         */
        public void onSuccessed(List<HomeGroups> beans);

        /**
         * 首页请求失败
         */
        public void onFailed();

    }
}
